using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using CharacterCreation.Data.Models;
using CharacterCreation.Data.Services;
using CharacterCreation.Views.Contracts;
using Microsoft.Practices.Prism.Commands;

namespace CharacterCreation.Views {
    /// <summary>
    /// The character new statistics view model
    /// </summary>
    public partial class CharacterNewStatisticsViewModel : BaseViewModel {

        private CharacterService _characterService = (CharacterService)CharacterService.CreateInstance();

        /// <summary>
        /// The new character
        /// </summary>
        public Character Character {
            get {
                return _characterService.NewItem;
            }
        }

        /// <summary>
        /// Gets or sets the StatisticsLeft
        /// </summary>
        public ObservableCollection<int> StatisticsLeft {
            get { return _StatisticsLeft; }
            set {
                _StatisticsLeft = value;
                RaisePropertyChanged("StatisticsLeft");
                RaisePropertyChanged("LeftTotal");
            }
        }
        private ObservableCollection<int> _StatisticsLeft;

        
        /// <summary>
        /// Gets or sets the StatisticsRight
        /// </summary>
        public ObservableCollection<int> StatisticsRight {
            get { return _StatisticsRight; }
            set {
                _StatisticsRight = value;
                RaisePropertyChanged("StatisticsRight");
                RaisePropertyChanged("RightTotal");
            }
        }
        private ObservableCollection<int> _StatisticsRight;

        /// <summary>
        /// The total of the left column
        /// </summary>
        public int LeftTotal {
            get {
                return StatisticsLeft.Sum();
            }
        }

        /// <summary>
        /// The total of the right column
        /// </summary>
        public int RightTotal {
            get {
                return StatisticsRight.Sum();
            }
        }

        /// <summary>
        /// The reroll command
        /// </summary>
        public ICommand ReRollCommand {
            get {
                return new DelegateCommand(() => { ReRoll(); });
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public CharacterNewStatisticsViewModel() {
            this.StatisticsLeft = new ObservableCollection<int>(DiceService.Stats());
            this.StatisticsRight = new ObservableCollection<int>(DiceService.Stats());
        }

        /// <summary>
        /// Reroll the statistics
        /// </summary>
        public void ReRoll() {
            this.StatisticsLeft = new ObservableCollection<int>(DiceService.Stats());
            this.StatisticsRight = new ObservableCollection<int>(DiceService.Stats());
        }
    }
}